package kr.or.kead.daegu.global.InputUI;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.StringTokenizer;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import kr.hkit.framework.dao.LoadPropertyDAO;
import kr.hkit.framework.inputPanel.AbsSearchUIPanel;
import kr.or.kead.daegu.global.Config;
import kr.or.kead.daegu.global.DTO.Business;
import kr.or.kead.daegu.global.DTO.Emp;
import kr.or.kead.daegu.global.DTO.Product;
import kr.or.kead.daegu.global.DTO.SaleHist;
import kr.or.kead.daegu.global.Service.DbDao;

public class InputSaleMgnUI extends JPanel implements ActionListener {
	private static final long serialVersionUID = 1L;
	
	private AbsSearchUIPanel pSEmp;
	private AbsSearchUIPanel pSProcuct;
	private AbsSearchUIPanel pSBusiness;
	private JTextField tfDate;
	private JTextField tfSaleCnt;
	private JButton btnOk;
	private JTextField tfSalePrimeCost;
	private JTextField tfSaleCost;
	private JTextField tfDisCost;
	private JButton btnSave;
	
	private DbDao dao;

	public InputSaleMgnUI() {
		dao = new DbDao(LoadPropertyDAO.getConnection());
		initialize();
	}

	private void initialize() {
		setPreferredSize(new Dimension(560, 300));
		setLayout(new GridLayout(0, 1, 0, 0));

		pSEmp = new AbsSearchUIPanel(Config.LBL_LIST_COLNAME[0]) {
			protected void btnOkActionPerformed(ActionEvent e) {
				try {
					Emp emp = dao.selectCodeByCode(new Emp(firstPanel.getTfValue().getText().trim()));
					firstPanel.setTfValue(emp.getCode().toUpperCase());
					secondPanel.setTfValue(emp.getName());
					thirdPanel.setTfValue(emp.getGrade());
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, "사원코드 검색 불가");	
					setTfValue("");
					firstPanel.getTfValue().requestFocus();
				}
			}
			
		};
		add(pSEmp);

		pSProcuct = new AbsSearchUIPanel(Config.LBL_LIST_COLNAME[1]) {
			protected void btnOkActionPerformed(ActionEvent e) {
				try {
					Product product = dao.selectCodeByCode(new Product(firstPanel.getTfValue().getText().trim()));
					firstPanel.setTfValue(product.getpCode().toUpperCase());
					secondPanel.setTfValue(product.getpName());
					thirdPanel.setTfValue(String.valueOf(product.getRegprice()));
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, "제품코드 검색 불가");	
					setTfValue("");
					firstPanel.getTfValue().requestFocus();
				}			
			}
		};
		add(pSProcuct);

		pSBusiness = new AbsSearchUIPanel(Config.LBL_LIST_COLNAME[2]) {
			protected void btnOkActionPerformed(ActionEvent e) {
				try {
					Business business = dao.selectCodeByCode(new Business(firstPanel.getTfValue().getText().trim()));
					firstPanel.setTfValue(business.getbCode().toUpperCase());
					secondPanel.setTfValue(business.getbName());
					thirdPanel.setTfValue(business.getbGrade());
				} catch (Exception e1) {
					JOptionPane.showMessageDialog(null, "거래처코드 검색");	
					setTfValue("");
					firstPanel.getTfValue().requestFocus();
				}	
				
			}
		};
		add(pSBusiness);

		JPanel panel_3 = new JPanel();
		add(panel_3);
		panel_3.setLayout(new GridLayout(0, 7, 0, 0));

		JLabel lbldate = new JLabel("거래일자");
		lbldate.setHorizontalAlignment(SwingConstants.RIGHT);
		panel_3.add(lbldate);
		
		Calendar cal = new GregorianCalendar();
		String d = cal.get(Calendar.YEAR)+"/"+(cal.get(Calendar.MONTH)+1) + "/" + cal.get(Calendar.DATE); 
		tfDate = new JTextField(d);
		panel_3.add(tfDate);
		tfDate.setColumns(10);

		JPanel panel_5 = new JPanel();
		add(panel_5);
		panel_5.setLayout(new GridLayout(0, 7, 0, 0));

		JLabel lbtCnt = new JLabel("판매수량");
		lbtCnt.setHorizontalAlignment(SwingConstants.RIGHT);
		panel_5.add(lbtCnt);

		tfSaleCnt = new JTextField();
		tfSaleCnt.setColumns(10);
		panel_5.add(tfSaleCnt);

		btnOk = new JButton("확인");
		btnOk.addActionListener(this);
		panel_5.add(btnOk);

		JPanel panel_6 = new JPanel();
		add(panel_6);
		panel_6.setLayout(new GridLayout(0, 7, 0, 0));

		JLabel lblPriCost = new JLabel("판매단가");
		lblPriCost.setHorizontalAlignment(SwingConstants.RIGHT);
		panel_6.add(lblPriCost);

		tfSalePrimeCost = new JTextField();
		tfSalePrimeCost.setColumns(10);
		panel_6.add(tfSalePrimeCost);

		JPanel panel_7 = new JPanel();
		add(panel_7);
		panel_7.setLayout(new GridLayout(0, 7, 0, 0));

		JLabel lblSaleCost = new JLabel("판매금액");
		lblSaleCost.setHorizontalAlignment(SwingConstants.RIGHT);
		panel_7.add(lblSaleCost);

		tfSaleCost = new JTextField();
		tfSaleCost.setColumns(10);
		panel_7.add(tfSaleCost);

		JPanel panel_8 = new JPanel();
		add(panel_8);
		panel_8.setLayout(new GridLayout(0, 7, 0, 0));

		JLabel lblDiscnt = new JLabel("할인금액");
		lblDiscnt.setHorizontalAlignment(SwingConstants.RIGHT);
		panel_8.add(lblDiscnt);

		tfDisCost = new JTextField();
		tfDisCost.setColumns(10);
		panel_8.add(tfDisCost);

		JPanel panel_4 = new JPanel();
		add(panel_4);
		panel_4.setLayout(new GridLayout(0, 1, 0, 0));

		btnSave = new JButton("저장");
		btnSave.addActionListener(this);
		panel_4.add(btnSave);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == btnOk) {
			actionPerformedBtnOk(e);
		}
		if (e.getSource() == btnSave) {
			btnSaveActionPerformed(e);
		}
	}

	private void clearTf(){
		pSEmp.setTfValue("");
		pSProcuct.setTfValue("");
		pSBusiness.setTfValue("");

		tfDate.setText("");
		tfSaleCnt.setText("");
		tfSalePrimeCost.setText("");
		tfSaleCost.setText("");
		tfDisCost.setText("");
	}
	
	protected void btnSaveActionPerformed(ActionEvent e) {
		String empCode = pSEmp.getFstValue();
		String productCode = pSProcuct.getFstValue();
		String busCode = pSBusiness.getFstValue();
		
		String pDate = tfDate.getText().trim();
		
		StringTokenizer st = new StringTokenizer(pDate,"/");
		Calendar cal = new GregorianCalendar();
		int year = Integer.parseInt(st.nextToken());
		int month = Integer.parseInt(st.nextToken())-1;
		int date = Integer.parseInt(st.nextToken());
		cal.set(year, month, date);
		
		String sCnt = tfSaleCnt.getText().trim();
		String sPriCost = tfSalePrimeCost.getText().trim();
		String sCost = tfSaleCost.getText().trim();
		String sDisCost = tfDisCost.getText().trim();
		
		if (empCode.equals("")||productCode.equals("")||busCode.equals("")||
				pDate.equals("")||sCnt.equals("")||sPriCost.equals("")||
				sCost.equals("")||sDisCost.equals("")){
			JOptionPane.showMessageDialog(null, "빈칸이 존재합니다");
			return;
		}
		
		try {
			SaleHist sHist = new SaleHist(empCode, busCode, productCode, Integer.parseInt(sCnt), Integer.parseInt(sCost), cal.getTime());
			dao.insertItem(sHist);
			JOptionPane.showMessageDialog(null, "저장되었습니다");
			clearTf();
		} catch (Exception e1) {
			JOptionPane.showMessageDialog(null, e1.getMessage());
		}

	}
	
	protected void actionPerformedBtnOk(ActionEvent e) {
		int sCnt = Integer.parseInt(tfSaleCnt.getText().trim());
		int regPrice = Integer.parseInt(pSProcuct.getThrdValue());
		
		double salePri = regPrice  *  selectRateGrade(pSEmp.getThrdValue() , pSBusiness.getThrdValue());
		int salePricost = Integer.parseInt(String.format("%.0f", salePri));
		int saleCost = salePricost * sCnt;
		int saleDisCost = regPrice * sCnt - salePricost;
		
		tfSalePrimeCost.setText(salePricost+"");
		tfSaleCost.setText(saleCost+"");
		tfDisCost.setText(saleDisCost+"");
		btnSave.requestFocus();
	}

	private double selectRateGrade(String eGrade, String bGrade) {
		double eg=0.0;
		double bg=0.0;
		
		switch(eGrade){
		case "A": eg = 0.03;
			break;
		case "B": eg = 0.02;
			break;
		case "C": eg = 0.01;
			break;
		}
		
		switch(bGrade){
		case "L": bg = 0.04;
			break;
		case "M": bg = 0.03;
			break;
		case "S": bg = 0.02;
			break;
		}
		
		return eg+bg;
	}



}
